<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c job xs f">

  <xsl:import href="../style/base.xsl" />
  
  <xsl:output encoding="UTF-8"
    doctype-public="-//W3C//DTD HTML 4.01//EN"
    doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
  
  <xsl:param name="ref" select="'/localhost/system/work'"/>
  
  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Web Jobs</title>
        <link href="../css/global.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../script/E.js"><!-- --></script>
        <script type="text/javascript" src="../script/util.js"><!-- --></script>
      </head>
      <body>
        <h2>Web Jobs</h2>
        <xsl:apply-templates select="doc(concat('../../data/admin/WebJobs?ref=', $ref))/*"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="/*">
    <table border="1" cellspacing="0" cellpadding="1" style="font-size: 80%;">
      <thead>
        <tr>
          <th>Time</th>
          <th>Duration</th>
          <th>Memory</th>
          <th>Host</th>
          <th>URL</th>
          <th>Referer</th>
          <th>Browser</th>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="*">
          <xsl:sort select="@job" order="descending"/>
        </xsl:apply-templates>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="request">
    <tr>
      <td>
        <a href="../meta?ref={$ref}/{@job}">
          <xsl:value-of select="format-dateTime(xs:dateTime(@time), '[Y0001]-[M01]-[D01]&#160;[H01]:[m01]:[s01]')"/>
        </a>
      </td>
      <td>
        <xsl:call-template name="print_duration">
          <xsl:with-param name="duration" select="xs:duration(@duration)"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:value-of select="format-number(@freeMemory div 1024 div 1024, '0')"/>
        <xsl:text> (</xsl:text>
        <xsl:value-of select="format-number(@totalMemory div 1024 div 1024, '0')"/>
        <xsl:text>)</xsl:text>
      </td>
      <td><a href="http://ws.arin.net/whois/?queryinput={@remote-ip}"><xsl:value-of select="@remote-ip"/></a></td>
      <td>
        <xsl:call-template name="print_cut">
          <xsl:with-param name="str" select="url"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:choose>
          <xsl:when test="starts-with(referer, concat(url/@scheme, '://', url/@server))">
            <xsl:call-template name="print_cut">
              <xsl:with-param name="str" select="referer"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring(referer, 1, 40)"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <td>
        <xsl:value-of select="substring(agent, 1, 12)"/>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template name="print_cut">
    <xsl:param name="str"/>
    <xsl:choose>
      <xsl:when test="string-length($str) > 40">
        <xsl:text>...</xsl:text>
        <xsl:value-of select="substring($str, string-length($str) - 39, 40)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$str"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="print_duration">
    <xsl:param name="duration"/>
    <div style="float: right; font-size: 80%;">
      <xsl:value-of select="f:format-duration($duration)"/>
    </div>
    <xsl:variable name="seconds" select="f:total-seconds($duration)"/>
    <xsl:choose>
      <xsl:when test="$seconds > 5">
        <div style="background-color: red; width: {$seconds}px;">&#160;</div>
      </xsl:when>
      <xsl:otherwise>
        <div style="background-color: #00CC00; width: {$seconds * 20}px;">&#160;</div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>
